package com.MC.MCard.servlet;

import com.MC.MCard.dao.MerchantDaoImpl;
import com.MC.MCard.entity.Merchant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
@WebServlet("/Merchant/update")
public class MerchantUpdateServlet extends HttpServlet {

    private MerchantDaoImpl merchantDao = new MerchantDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mid = req.getParameter("mid");
        try {
            Merchant merchant = merchantDao.findById(Integer.parseInt(mid));
            req.setAttribute("merchant",merchant);
            req.getRequestDispatcher("/WEB-INF/views/merchant/update.jap");
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mid = req.getParameter("mid");
        String memail = req.getParameter("memail");
        String mname=req.getParameter("mname");
        String mpassword = req.getParameter("mpassword");

        try {
            merchantDao.update(Integer.parseInt(mid),memail,mname,mpassword);
            resp.sendRedirect(req.getContextPath()+"/");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
